load('../../src/chrome/content/folderplus-conversation.js');

var convListenerCount = 0;

function run_test() {
  
    var callbackCalled   = null;
	var callbackUICalled = new Array();
    
	function callback   (aFolderURI) {do_print('fake callback '   + aFolderURI); callbackCalled = aFolderURI;};
	function callbackUI (aFolderURI) {do_print('fake callbackui ' + aFolderURI); callbackUICalled.push(aFolderURI);};
	
	folderplus.conv._lastSearch.messageId = null;
	folderplus.conv._lastSearch.folderURI = null;
	folderplus.conv._lastSearch.timestamp = null;

	var aMsgHdr = { messageId : 12,
                    folder : { URI : "folder 1" },
                    date : new Date(2013,2,9,10,30),
                    mime2DecodedSubject : "prefixe : sujet1" };

	var aGloda = makeGloda("conv", "folder 2", 0);
					   
	folderplus.conv._getFolder(aMsgHdr, callback, aGloda, callbackUI);

    do_check_eq(callbackCalled,      "folder 2");
    do_check_eq(callbackUICalled[1], "folder 2");
    do_check_eq(convListenerCount,  1);
    
    convListenerCount = 0;

  	folderplus.conv._getFolder(aMsgHdr, callback, aGloda, callbackUI);
  
    do_check_eq(callbackCalled,      "folder 2");
    do_check_eq(callbackUICalled[1], "folder 2");
    do_check_eq(convListenerCount,  0);
  
    sleep(12000);
  
    convListenerCount = 0;
					
	folderplus.conv._getFolder(aMsgHdr, callback, aGloda, callbackUI);
	
    do_check_eq(callbackCalled,      "folder 2");
    do_check_eq(callbackUICalled[1], "folder 2");
    do_check_eq(convListenerCount,  1);
}

function makeGloda(type, folder, wait) {
	if (type == "conv") {
		return {
			getMessageCollectionForHeader : function (aMsgHdr, aListener) {
				var aGlodaMessage = {
					conversation : {
						getMessagesCollection : function(aConvListener) {
							var aGlodaMessage2  = { folderMessage : { folder : { URI : folder },
																	  date : new Date(2013,2,9,10,30) },
													subject : "sujet1" };
							var aGlodaMessage22 = { folderMessage : { folder : { URI : folder },
																	  date : new Date(2013,2,8,9,0) },
													subject : "sujet2" };
							var aCollection2 = { items : [ aGlodaMessage2, aGlodaMessage22 ] };
							// attente dans la recherche par conversation
							sleep(wait);
							aConvListener.onQueryCompleted(aCollection2);
						}
					}
				};
				var aCollection = { items : [ aGlodaMessage ] };
                convListenerCount += 1;
				aListener.onQueryCompleted(aCollection);
			},
			newQuery : function (aParam) {
				var aQuery = {
					subjectMatches : function(aSubject) {},
					getCollection : function(aSubjectListenerExact) {}
				};
				return aQuery;
			}
		};
	} else if (type == "subject") {
		return {
			getMessageCollectionForHeader : function (aMsgHdr, aListener) {
				var aGlodaMessage = {
					conversation : {
						getMessagesCollection : function(aConvListener) {
							var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																	  date : new Date(2013,2,9,10,30) },
													subject : "sujet1" };
							var aCollection2 = { items : [ aGlodaMessage2 ] };
							aConvListener.onQueryCompleted(aCollection2);
						}
					}
				};
				var aCollection = { items : [ aGlodaMessage ] };
				aListener.onQueryCompleted(aCollection);
			},
			newQuery : function (aParam) {
				var aQuery = {
					subjectMatches : function(aSubject) {},
					getCollection : function(aSubjectListenerExact) {
						var aGlodaMessage  = { folderMessage : { folder : { URI : "folder 3" },
																  date : new Date(2013,2,8,10,30) },
											   subject : "sujet3" };
						var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 3" },
																  date : new Date(2013,2,7,10,30) },
												subject : "re: sujet3" };
						var aCollection = { items : [ aGlodaMessage, aGlodaMessage2 ] };
						// attente dans la recherche par conversation
						sleep(wait);
						aSubjectListenerExact.onQueryCompleted(aCollection);
					}
				};
				return aQuery;
			},
			subjectMatches : function (aSubject) {},
		};
	} else {
		return {
			getMessageCollectionForHeader : function (aMsgHdr, aListener) {
				var aGlodaMessage = {
					conversation : {
						getMessagesCollection : function(aConvListener) {
							var aGlodaMessage2  = { folderMessage : { folder : { URI : "folder 1" },
																	  date : new Date(2013,2,9,10,30) },
													subject : "sujet1" };
							var aCollection2 = { items : [ aGlodaMessage2 ] };
							aConvListener.onQueryCompleted(aCollection2);
						}
					}
				};
				var aCollection = { items : [ aGlodaMessage ] };
				aListener.onQueryCompleted(aCollection);
			},
			newQueryCall : 0,
			newQuery : function (aParam) {
				var aQuery = {
					subjectMatches : function(aSubject) {},
					getCollection : function(aSubjectListenerExact) {
						var aGlodaMessage  = { folderMessage : { folder : { URI : "folder 3" },
																  date : new Date(2013,2,8,10,30) },
											   subject : "sujet3" };
						var aCollection = { items : [ aGlodaMessage ] };
						aSubjectListenerExact.onQueryCompleted(aCollection);
					},
				};
				var aQuery2 = {
					subjectMatches : function(aSubject) {},
					getCollection : function(aSubjectListenerExact) {
						var aGlodaMessage  = { folderMessage : { folder : { URI : "folder 3" },
																  date : new Date(2013,2,8,10,30) },
											   subject : "sujet3" };
						var aCollection = { items : [ aGlodaMessage ] };
						// attente dans la recherche par conversation
						sleep(wait);
						aSubjectListenerExact.onQueryCompleted(aCollection);
					}
				}
				if (this.newQueryCall == 0) {
					this.newQueryCall++;
					return aQuery;	
				} else {
					return aQuery2;
				}
			},
			subjectMatches : function (aSubject) {},
		};
	}
}

function setTimeout(func, delay) {
	do_timeout(delay, func);
	do_test_pending();
}

function sleep(milliseconds) {
  // We basically just call this once after the specified number of milliseconds
  var timeup = false;
  function wait() { timeup = true; }
  do_timeout(milliseconds, wait);

  var thread = Components.classes["@mozilla.org/thread-manager;1"].
               getService().currentThread;
  while(!timeup) {
    thread.processNextEvent(true);
  }
  do_test_finished();
}

folderplus.log = function (arg) {do_print(arg);};
